# Import some standard Python network libraries.
import urllib, urllib2

# Import the jinja2 templating engine for XML production.
from hsvo.jinja2 import Environment, FileSystemLoader

# Import some deployment-specific constants.
from hsvo.constants import SAVOIR_ENDPOINT, SERVICE_ID, BI_DEBUG

class HSVOSavoirInterface:
    """
    HSVOSavoirInterface()
    
    An interface to SAVOIR, allowing other classes to send 
    a pre-defined set of messages to SAVOIR as required.
    """
    
    def __init__(self):
        self.templateEnvironment = Environment(loader=FileSystemLoader('templates'))
        self.templateEnvironment.globals.update({'SAVOIR_ENDPOINT': SAVOIR_ENDPOINT, 'SERVICE_ID': SERVICE_ID})

    def sendStatus(self, sessionID, user, deviceParameters):
        """
        sendStatus(sessionID, user, deviceParameters) - This method is 
        responsible for sending an HSVO "status" message to SAVOIR.
        
        Throws:
            -HTTPError if the connection to SAVOIR fails.
        """
            
        # Generate the HSVO XML using the status template.
        template = self.templateEnvironment.get_template('savoir_status.xml')
        hsvoXml = template.render(
            savoirUser=user,
            sessionID=sessionID,
            parameters=deviceParameters
        )
        
        # If debugging is enabled, write the outgoing message to the terminal.
        if BI_DEBUG:
            print '\nSending status message to SAVOIR...'
            print('==== Status Message ====================================\n')
            print(hsvoXml+'\n')
        
        return self.__send(hsvoXml)

    def acknowledge(self, sessionID, user, action):
        """
        acknowledge(sessionID, user, action) - This method is 
        responsible for sending an HSVO "acknowledge" message to SAVOIR.
        
        Throws:
            -HTTPError if the connection to SAVOIR fails.
        """
            
        # Generate the HSVO XML using the acknowledge template.
        template = self.templateEnvironment.get_template('savoir_acknowledge.xml')
        hsvoXml = template.render(
            savoirUser=user,
            sessionID=sessionID,
            action=action
        )
        
        # If debugging is enabled, write the outgoing message to the terminal.
        if BI_DEBUG:
            print '\nSending acknowledge message to SAVOIR...'
            print('==== Acknowledge Message ===============================\n')
            print(hsvoXml+'\n')
        
        return self.__send(hsvoXml)
    
    def launch(self, sessionID, user, launchUrl):
        """
        launch(sessionID, user, launchUrl) - This method is 
        responsible for sending an HSVO "launch" message to SAVOIR.
        
        Throws:
            -HTTPError if the connection to SAVOIR fails.
        """
            
        # Generate the HSVO XML using the launch template.
        template = self.templateEnvironment.get_template('savoir_launch.xml')
        hsvoXml = template.render(
            savoirUser=user,
            sessionID=sessionID,
            launchUrl=launchUrl
        )
        
        # If debugging is enabled, write the outgoing message to the terminal.
        if BI_DEBUG:
            print '\nSending launch message to SAVOIR...'
            print('==== Launch Message ====================================\n')
            print(hsvoXml+'\n')
        
        return self.__send(hsvoXml)
        
    def __send(self, hsvoXml):
        """
        send(hsvoXml) - This private method is responsible for submitting POST 
        data (messages) to SAVOIR.
        """
        
        # Create an HTTP connection to SAVOIR and send the HSVO XML message via POST.
        usock = urllib2.urlopen(SAVOIR_ENDPOINT, urllib.urlencode({'savoirmsg': hsvoXml}), 10)
        usock.close()
        
        return True